const Service = require("egg").Service;

class GroupService extends Service {

    async getList() {
        const { ctx, app } = this;
        let {
            pageIndex,
            pageSize,
            p,
            n,
            s,
            keyword,
            status,
            state
        } = ctx.request.body;
        let nb = n === "市辖区" ? "" : n;
        let address = p + nb + s;

        let sql = "select * from grouplist where id=id "
        if (address) {
            sql += `and address like "%${address}%" `
        }
        if (keyword) {
            sql += `and name like "%${keyword}%" `
        }

        if (status) {
            sql += `and status="${status}" `
        }

        if (state !== undefined && state !== "") {
            state = state === "正常" ? 1 : 0;
            sql += `and state=${state} `
        }
        console.log(sql);

        let res = await app.mysql.query(sql);
        let data = res.slice((pageIndex - 1) * pageSize, pageIndex * pageSize);
        return {
            total: res.length,
            data
        }
    }
    //删除
    async del() {
        const { ctx, app } = this;
        const { id } = JSON.parse(ctx.query.params);
        let res = await app.mysql.delete("grouplist", { id });
        return res;
    }
    //编辑
    async edit() {
        const { ctx, app } = this;
        const { id, state } = ctx.request.body;
        let res = await app.mysql.update("grouplist", { id, state });
        return res;
    }
}
module.exports = GroupService;